package cn.itcast.web.controller.cargo;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.util.StrUtil;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.PackingExample;
import cn.itcast.domain.cargo.Shipping;
import cn.itcast.domain.cargo.ShippingExample;
import cn.itcast.domain.vo.ContractProductVo;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.service.cargo.ShippingService;
import cn.itcast.util.DownloadUtils;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/cargo/shipping",name = "委托管理")
public class ShippingController extends BaseController{

    @Reference
    private ShippingService shippingService;

    @Reference
    private PackingService packingService;

    @RequestMapping(value = "/list",name ="委托分页查询" )
    public String list(@RequestParam(defaultValue = "5") Integer pageSize , @RequestParam(defaultValue = "1") Integer pageNum){

        ShippingExample shippingExample = new ShippingExample();
        ShippingExample.Criteria criteria = shippingExample.createCriteria();
        PageInfo page = shippingService.findByPage(pageNum,pageSize,shippingExample);
        request.setAttribute("page",page);
        return "cargo/shipping/shipping-list";
    }

    @RequestMapping(value = "/toUpdate",name = "去修改页面")
    public String toUpdate(String id){
        Shipping shipping = shippingService.selectByPrimaryKey(id);
        System.out.println(shipping);
        request.setAttribute("shipping",shipping);
        return "cargo/shipping/shipping-update";
    }

    @RequestMapping(value = "/edit",name = "新增或修改")
    public String edit(Shipping shipping,String packingListId){
        Packing packing = packingService.selectByPrimaryKey(packingListId);
        packing.setState(2);
        packingService.update(packing);

        shipping.setCreateBy(getLoginUser().getUserName());
        shipping.setCreateDept(getLoginUser().getDeptName());
        shipping.setCreateTime(new Date());
        if (StrUtil.isEmpty(shipping.getShippingOrderId())) { // 新增报运单
            shippingService.save(shipping,packingListId);
        }else{ //更新报运单
            shippingService.update(shipping);
        }
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping(value = "/delete",name = "删除")
    public String delete(String id){
        shippingService.deleteByPrimaryKey(id);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping(value = "/submit",name = "提交")
    public String submit(String id){
        Shipping shipping = shippingService.selectByPrimaryKey(id);
        shipping.setState(1);
        shippingService.update(shipping);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping(value = "/cancel",name = "取消")
    public String cancel(String id){
        Shipping shipping = shippingService.selectByPrimaryKey(id);
        shipping.setState(0);
        shippingService.update(shipping);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping(value = "/printExcelWithTemplate", name = "excel模板下载")
    public void printExcelWithTemplate(String id) throws Exception {
        Shipping shipping = shippingService.selectByPrimaryKey(id);
        // ii.使用easyPOI将通过模板将数据封装为Workbook
        // 1.加载模板
        String realPath = session.getServletContext().getRealPath("make/xlsprint/6-海运委托书.xlsx");// 获取模板真实路径
        TemplateExportParams params = new TemplateExportParams(realPath);
        // 2.准备数据（模型）
        Map map = new HashedMap();
        map.put("title", "海运委托书");
        map.put("shipper",shipping.getShipper() );
        map.put("consignee",shipping.getConsignee() );
        map.put("notifyParty",shipping.getNotifyParty() );
        map.put("lcNo",shipping.getLcNo() );
        map.put("portOfLoading",shipping.getPortOfLoading() );
        map.put("portOfTrans",shipping.getPortOfTrans() );
        map.put("portOfDischarge",shipping.getPortOfDischarge() );
        map.put("loadingDate",shipping.getLoadingDate() );
        map.put("limitDate",shipping.getLimitDate() );
        map.put("isBatch",shipping.getIsBatch() );
        map.put("isTrans",shipping.getIsTrans() );
        map.put("copyNum",shipping.getCopyNum() );
        map.put("specialCondition",shipping.getSpecialCondition() );
        map.put("createTime",shipping.getCreateTime() );
        // 3.封装为workbook
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);

        // iii.使用工具类完成文件下载
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        workbook.write(byteArrayOutputStream);
        DownloadUtils.download(byteArrayOutputStream, response, "海运委托书.xlsx");

    }

    @RequestMapping(value = "/toShipping",name = "发票新增页面")
    public String toShipping(String id){
        request.setAttribute("id",id);
        return "cargo/invoice/invoice-toInvoice";
    }

}
